<template>
  <div>
    <el-row>
      <el-col :span="4">
        <el-menu
          default-active="2"
          class="el-menu-vertical-demo"
          @select="handleSelect"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b">
          <el-submenu :index="value.name" v-for="(value, key, index) in resourceTree()" :key="value.name">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{value.name}}</span>
            </template>
            <el-menu-item :index="value.code" v-for="(value, key, index) in value.ssoResourceTreeViews"
                          :key="value.name">{{value.name}}
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-col>

      <el-col :span="20">
        <el-container>
          <el-header style="text-align: right; font-size: 14px;">
            <el-dropdown style="margin-top: 10px;">
              <i class="el-icon-setting"><span style="margin-left: 10px">{{subject() ? subject().name:""}}</span></i>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>
                  <span @click="logout()">退出<i class="el-icon-switch-button el-icon--right"/></span>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-header>
          <el-main>
            <router-view></router-view>
          </el-main>
        </el-container>
      </el-col>

    </el-row>
  </div>
</template>

<script>
  import authorityService from '../service/AuthorityService.js';
  import utilService from '../service/UtilService.js';

  export default {
    name: "Index",
    methods: {
      handleSelect(key, keyPath) {
        if (utilService.startsWith(key, "http://")) {
          let targetPath = key + ((key.indexOf("?") > -1) ? ("&code=" + authorityService.getCode()) : ("?code=" + authorityService.getCode()));
          window.location.href = targetPath;
        } else {
          this.$router.push({name: key})
        }
      },
      checkAuthority(locator) {
        return authorityService.checkAuthority(locator)
      },
      subject() {
        return authorityService.subject();
      },
      resourceTree() {
        return authorityService.resourceTree()
      },
      logout() {
        authorityService.logout(this.$http);
      }
    }
  }
</script>

<style>

</style>
